Imports System.Configuration
Imports System.Web.Configuration
Imports System.Web

Public Class CodeDecodeService
    Private Shared _provider As CodeDecodeProvider = Nothing
    Private Shared _providers As CodeDecodeCollection = Nothing
    Private Shared _lock As Object = New Object
    Private Shared isInitialized As Boolean = False
    Private Shared initializationException As Exception

    Public ReadOnly Property Provider() As CodeDecodeProvider
        Get
            Return _provider
        End Get
    End Property

    Public ReadOnly Property Providers() As CodeDecodeCollection
        Get
            Return _providers
        End Get
    End Property

    Public Shared Function GetAllCodeCategories() As System.Data.DataTable
        LoadProviders()
        Return _provider.GetAllCodeCategories
    End Function

    Public Shared Function GetCodeList() As System.Data.DataTable
        LoadProviders()
        Return _provider.GetCodeList
    End Function

    Public Shared Function GetCodeList(ByVal CategoryId As Integer) As System.Data.DataTable
        LoadProviders()
        Return _provider.GetCodeList(CategoryId)
    End Function

    Public Shared Function GetCodeList(ByVal CategoryId As Integer, ByVal RetrieveForDropdown As Boolean) As System.Data.DataTable
        LoadProviders()
        Return _provider.GetCodeList(CategoryId, RetrieveForDropdown)
    End Function

    Public Shared Function GetLongDesc(ByVal CodeCatId As Integer, ByVal CodeId As Integer) As String
        LoadProviders()
        Return _provider.GetLongDesc(CodeCatId, CodeId)
    End Function

    Public Shared Function GetLongDesc(ByVal CodeCatId As Integer, ByVal CodeValue As String) As String
        LoadProviders()
        Return _provider.GetLongDesc(CodeCatId, CodeValue)
    End Function

    Public Shared Function GetShortDesc(ByVal CodeCatId As Integer, ByVal CodeId As Integer) As String
        LoadProviders()
        Return _provider.GetShortDesc(CodeCatId, CodeId)
    End Function

    Public Shared Function GetShortDesc(ByVal CodeCatId As Integer, ByVal CodeValue As String) As String
        LoadProviders()
        Return _provider.GetShortDesc(CodeCatId, CodeValue)
    End Function

    Public Shared Function GetUserSettings(ByVal UserId As String) As System.Collections.Hashtable
        LoadProviders()
        Return _provider.GetUserSettings(UserId)
    End Function

    Private Shared Sub LoadProviders()
        Try
            'Get the feature's configuration info
            Dim qc As CodeDecodeConfiguration = CType(ConfigurationManager.GetSection("CodeDecodeProvider"), CodeDecodeConfiguration)
            If ((qc.DefaultProvider Is Nothing) _
                        OrElse ((qc.Providers Is Nothing) _
                        OrElse (qc.Providers.Count < 1))) Then
                Throw New System.Configuration.Provider.ProviderException(("You must specify a valid default provider."))
            End If
            _providers = New CodeDecodeCollection
            ProvidersHelper.InstantiateProviders(qc.Providers, _providers, GetType(CodeDecodeProvider))
            _providers.SetReadOnly()
            _provider = _providers(qc.DefaultProvider)
            If (_provider Is Nothing) Then
                Throw New ConfigurationErrorsException("You must specify a default provider for the feature.", qc.ElementInformation.Properties("defaultProvider").Source, qc.ElementInformation.Properties("defaultProvider").LineNumber)
            End If
        Catch ex As Exception
            initializationException = ex
            isInitialized = True
            Throw ex
        End Try
        isInitialized = True
        'error-free initialization
    End Sub
End Class
